const query = require("../db/query");
var md5 = require("md5");
const cmsuser = async (ctx) => {
  // -- 超级管理员登录
  const sql = `SELECT 
    cmsuser.username,
    cmsuser.password,
    authority.routeyi,
    authority.routeer,
    authority.nameyi,
    authority.nameer
FROM 
    cmsuser
INNER JOIN 
    authority ON cmsuser.authid = authority.userid
		WHERE cmsuser.username = '${ctx.username}' AND cmsuser.password = '${ctx.password}';
        `;
  return await query(sql);
};
// reac查询所有医生信息
const doctorFind = async (ctx) => {
  const sql = `SELECT * FROM authority WHERE userid = 2`;
  return await query(sql);
};
// 医生登录
const doctorlogin = async (ctx) => {
  const sql = `SELECT * FROM homeall WHERE name='${ctx.name}'  AND  identitycard='${ctx.identitycard}'`;
  return await query(sql);
};
// 管理医生添加
const doctoradd = async (ctx) => {
  // console.log(ctx,'同意入驻后医生添加字段');
  const sql = `INSERT INTO homeall (city,administrativeOffice,state,name,nosocomium,department,img,identitycard) values ('${ctx.city}', '${ctx.administrativeOffice}','${ctx.state}','${ctx.name}','${ctx.nosocomium}','${ctx.department}','${ctx.img}','${ctx.identitycard}')`;
  return await query(sql);
};
// 管理医生删除
const doctordel = async (ctx) => {
  const sql = `DELETE FROM homeall WHERE id in (${ctx.id})`;
  return await query(sql);
};
// 管理医生修改
const doctorupdate = async (ctx) => {
  const sql = `	UPDATE homeall SET city='${ctx.city}',administrativeOffice='${ctx.administrativeOffice}',state='${ctx.state}',name='${ctx.name}',nosocomium='${ctx.nosocomium}',department='${ctx.department}',img='${ctx.img}',identitycard='${ctx.identitycard}' WHERE id='${ctx.id}'
`;
  return await query(sql);
};
// 模糊搜索查找医生
const doctorfind = async (ctx) => {
  const sql = `select * from homeall where name like '%${ctx.name}%'`;
  return await query(sql);
};
// 分页
const doctorall = async (ctx) => {
  let sql = `SELECT * FROM homeall`;
  //  分页
  let sum = await query(sql); //所有数据
  let num = sum.length; //总数据的长度
  // page当前页码  size每页显示的条数
  sql += ` limit  ${(ctx.page - 1) * ctx.size},${ctx.size}`; //分页
  let data = await query(sql);
  return { data, num };
};
// 查找医生
const cha = async (ctx) => {
  let sql = `SELECT * FROM doctoraudit WHERE name='${ctx.name}'`;
  return await query(sql);
};
//=================================================================
// 审核查找 
const doctorauditfind = async (ctx) => {
  let sql = `SELECT * FROM doctoraudit`;
  return await query(sql);
  };

// 审核删除 
const doctorauditdel = async (ctx) => {
  let sql = `DELETE FROM doctoraudit WHERE id in (${ctx.id})`;
  return await query(sql);
};

// 微医点击申请入驻
const doctorauditadd = async (ctx) => {
  let sql = `INSERT INTO doctoraudit (city,administrativeOffice,state,name,nosocomium,department,img,identitycard,flag) values ('${ctx.city}','${ctx.administrativeOffice}','${ctx.state}','${ctx.name}','${ctx.nosocomium}','${ctx.department}','${ctx.img}','${ctx.identitycard}','待审核')`;
  return await query(sql);
};
// 审核不同意修改flag=已拒绝
const doctorauditupdate = async (ctx) => {
  let sql = `UPDATE doctoraudit SET flag = '已拒绝' WHERE id ='${ctx.id}'`;
  return await query(sql);
};
// 审核通过修改flag=审核成功
const doctorauditpass = async (ctx) => {
  let sql = `UPDATE doctoraudit SET flag = '审核成功' WHERE id ='${ctx.id}'`;
  return await query(sql);
};
//微医点击查看审核结果
const doctorauditresult = async (ctx) => {
  let sql = `SELECT * FROM doctoraudit WHERE id='${ctx.id}'`;
  return await query(sql);
  };
module.exports = {
  cmsuser,
  doctorFind,
  doctorlogin,
  doctoradd,
  doctordel,
  doctorfind,
  doctorall,
  cha,
  doctorupdate,
  doctorauditdel,
  doctorauditadd,
  doctorauditupdate,
  doctorauditpass,
  doctorauditfind,doctorauditresult
};

